package com.gm.corejava.book1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Administrator on 14-1-7.
 */
public class GridBagFrame extends JFrame {
        public GridBagFrame(){
            setTitle("Grid Bag Layout Test");
            setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

            setLayout(new GridBagLayout());

            ActionListener fontAction=new FontAction();

            JLabel fontLabel=new JLabel("Font:");
            String[] fontnames= new String[]{"Serif","SansSerif","Monospaced","Dialog","DialogInput"};
            face=new JComboBox<String>(fontnames);
            face.addActionListener(fontAction);

            JLabel sizeLabel=new JLabel("Size");
            String[] fontsizes=new String[]{"8","10","12","15","18","24","36","48"};
            size=new JComboBox<String>(fontsizes);
            size.addActionListener(fontAction);

            bold=new JCheckBox("Bold");
            bold.addActionListener(fontAction);

            italic=new JCheckBox("Italic");
            italic.addActionListener(fontAction);


            sample=new JTextArea();
            sample.setText("Some Sample Text");
            sample.setEditable(false);
            sample.setLineWrap(true);
            sample.setBorder(BorderFactory.createEtchedBorder());

            add(fontLabel,new GBC(0,0).setAhchor(GBC.EAST));
            add(face,new GBC(1,0).setFill(GBC.HORIZONTAL).setInsets(1).setWeight(100,0));
            add(sizeLabel,new GBC(0,1).setAhchor(GBC.EAST));
            add(size,new GBC(1,1).setFill(GBC.HORIZONTAL).setInsets(1).setWeight(100,0));
            add(bold,new GBC(0,2,2,1).setAhchor(GBC.CENTER).setWeight(100,100));
            add(italic,new GBC(0,3,2,1).setAhchor(GBC.CENTER).setWeight(100,100));
            add(sample,new GBC(2,0,1,4).setFill(GBC.BOTH).setWeight(100,100));
        }
        private final int DEFAULT_WIDTH=400;
        private final int DEFAULT_HEIGHT=400;
        private JComboBox<String> face;
        private JComboBox<String> size;
        private JCheckBox bold;
        private JCheckBox italic;
        private JTextArea sample;

        private class FontAction implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                String fontFace= (String) face.getSelectedItem();
                int fontStyle=(bold.isSelected()?Font.BOLD:0)+(italic.isSelected()?Font.ITALIC:0);
                int fontSize=Integer.parseInt((String) size.getSelectedItem());

                Font font=new Font(fontFace,fontStyle,fontSize);
                sample.setFont(font);
                sample.repaint();
            }
        }
    }
